/**
 * @Title: MailWebController.java
 * @Package com.chanjet.gongzuoquan.mail.rest.controller
 * @Description: 邮局操作web接口类
 * @author huqw
 * @date 2016年3月21日 上午8:48:42
 */
package com.ydd.wrench.server.controller;

import com.cellplus.comm.util.*;
import com.wrench.util.WrUtil;
import com.ydd.wrench.bean.base.BaseData;
import com.ydd.wrench.bean.base.CodeRule;
import com.ydd.wrench.form.server.BaseDataService;
import com.ydd.wrench.form.server.CodeRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author huqw
 *
 */
@Controller("baseServiceController")
@RequestMapping("/ser")
public class BaseServiceController {

  protected final static Logger logger = LoggerFactory.getLogger(BaseServiceController.class);

  @Resource
  private BaseDataService baseDataSer;

  @Resource
  private CodeRuleService codeRuleSer;

  @RequestMapping(value = "/base/getDemoCode")
  @ResponseBody
  public Map<String, Object> getDemoCode(HttpServletRequest request,
      @RequestParam(value = "jsonObj", required = true) String jsonObj,
      @RequestParam(value = "num", required = false, defaultValue = "8") int num, HttpServletResponse response) throws Exception {
    CodeRule rule = ImpBeanUtil.mapObjToObjT(ImpJsonUtil.mapObjFromJson(ImpJsonUtil.ParseJson(jsonObj), null, null),
        CodeRule.class, null, null, 0, null);
    return ErrorInfo.Sucess.toBaseResult().setMapData(codeRuleSer.getDemoCode(rule, num)).getMap();
  }

  @RequestMapping(value = "/base/addOrUpdateCode")
  @ResponseBody
  public Map<String, Object> testdb(HttpServletRequest request,
      @RequestParam(value = "jsonObj", required = true) String jsonObj, HttpServletResponse response) throws Exception {
    return codeRuleSer.addOrUpdateCodeStr(jsonObj).getMap();
  }

  @RequestMapping(value = "/base/getCode")
  @ResponseBody
  public Map<String, Object> getCode(HttpServletRequest request,
      @RequestParam(value = "codeId", required = true) String codeId, HttpServletResponse response) throws Exception {
    return codeRuleSer.getCode(codeId).toRetMapObj();
  }

  @RequestMapping(value = "/base/deleteCode")
  @ResponseBody
  public Map<String, Object> deleteCode(HttpServletRequest request,
      @RequestParam(value = "codeId", required = true) String codeId, HttpServletResponse response) throws Exception {
    return codeRuleSer.deleteCode(codeId).getMap();
  }

  @RequestMapping(value = "/base/getNextCode")
  @ResponseBody
  public Map<String, Object> getNextCode(HttpServletRequest request,
      @RequestParam(value = "codeId", required = true) String codeId, HttpServletResponse response) throws Exception {
    return codeRuleSer.getNextCode(codeId).getMap();
  }

  @RequestMapping(value = "/base/addOrUpdBaseDataItem")
  @ResponseBody
  public Map<String, Object> addOrUpdBaseDataItem(HttpServletRequest request,
      @RequestParam(value = "jsonObj", required = true) String jsonObj, HttpServletResponse response) throws Exception {
      BaseData bd = ImpJsonUtil.objFromJson2Str(jsonObj, BaseData.class, null, null, null);
      return baseDataSer.addOrUpdBaseDataItem(bd).getMap();
  }

  @RequestMapping(value = "/base/getGroupBaseData")
  @ResponseBody
  public Map<String, Object> getGroupBaseData(HttpServletRequest request,
      @RequestParam(value = "groupName", required = true) String groupName,
      @RequestParam(value = "dataShowType", required = true, defaultValue = "10") int showType,
      @RequestParam(value = "genTree", required = true, defaultValue = "0") int genTree,
      HttpServletResponse response) throws Exception {
    ErrorInfo err = baseDataSer.getGroupBaseData(groupName, showType);
    if ( genTree ==1 ){
      err.setData(ImpMapObjUtil.lmToTreeMap(err.<Map<String,Object>>getDataListT(), "itemId","parentId","children", null));
    }
    return err.toRetMapObj();
  }

  @RequestMapping(value = "/base/getGroupBaseDataOfParent")
  @ResponseBody
  public Map<String, Object> getGroupBaseDataOfParent(HttpServletRequest request,
      @RequestParam(value = "groupName", required = true) String groupName,
      @RequestParam(value = "dataShowType", required = true, defaultValue = "10") int showType,
      @RequestParam(value = "parentId", required = true, defaultValue = "10") String parentId,
      @RequestParam(value = "genTree", required = true, defaultValue = "0") int genTree,
      HttpServletResponse response) throws Exception {
    ErrorInfo err = baseDataSer.getGroupBaseDataOfParent(groupName, parentId, showType);
    if ( genTree ==1 ){
      err.setData(ImpMapObjUtil.lmToTreeMap(err.<Map<String,Object>>getDataListT(), "itemId","parentId","children", null));
    }
    return err.toRetMapObj();
  }
  @RequestMapping(value = "/base/getBaseDataItem")
  @ResponseBody
  public Map<String, Object> getBaseDataItem(HttpServletRequest request,
      @RequestParam(value = "baseId", required = true) String baseId, HttpServletResponse response) throws Exception {
    return baseDataSer.getBaseDataItem(baseId).toRetMapObj();
  }

  @RequestMapping(value = "/base/deleteBaseDataItem")
  @ResponseBody
  public Map<String, Object> deleteBaseDataItem(HttpServletRequest request,
      @RequestParam(value = "baseId", required = true) String baseId, HttpServletResponse response) throws Exception {
    return baseDataSer.deleteBaseDataItem(baseId).toRetMapObj();
  }

  @RequestMapping(value = "/base/deleteGroupBaseData")
  @ResponseBody
  public Map<String, Object> deleteGroupBaseData(HttpServletRequest request,
      @RequestParam(value = "groupName", required = true) String groupName, HttpServletResponse response) throws Exception {
    return baseDataSer.deleteGroupBaseData(groupName).getMap();
  }

  @RequestMapping(value = "/base/getAllGroupBaseData")
  @ResponseBody
  public Map<String, Object> getAllGroupBaseData(HttpServletRequest request,
      @RequestParam(value = "pageNum", required = false, defaultValue = "0") int pageNum,
      @RequestParam(value = "perPageNum", required = false, defaultValue = "0") int perPageNum,
      HttpServletResponse response) throws Exception {
    return baseDataSer.getAllGroupBaseData(pageNum, perPageNum).toRetMapObj();
  }
}
